package com.vincent.kdp.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.vincent.kdp.Activity.ReceiveActivity;
import com.vincent.kdp.Activity.WaitConfirmListActivity;
import com.vincent.kdp.R;
import com.vincent.kdp.bean.GoodsListBean;
import com.vincent.kdp.ui.bean.response.WaitConfirmListOutput;

import java.util.List;

/**
 * Created by jingjing on 2017/2/20.
 */

public class GetGoodsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ReceiveActivity context;
    private List<GoodsListBean.MyGoodsData.MyGoodsList>  data;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    public GetGoodsListAdapter(ReceiveActivity context, List data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view= LayoutInflater.from(context).inflate(R.layout.my_goods_list_adapter,parent,false);
            return new GoodsViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position<data.size()){
            final GoodsListBean.MyGoodsData.MyGoodsList c = data.get(position);
            if (holder instanceof GoodsViewHolder) {
                //holder.tv.setText(data.get(position));
                GoodsViewHolder itemViewHolder=(GoodsViewHolder)holder;
                itemViewHolder.code.setText(c.getBarCode());
                itemViewHolder.name_good.setText(c.getName());
                if("1".equals(c.getChargeUnit())){
                    c.setPriceOrWeight(c.getTotalWight()+"克");
                }else{
                    c.setPriceOrWeight(String.valueOf(c.getPrice())+"元");
                }
                itemViewHolder.unit.setText(c.getPriceOrWeight());
                itemViewHolder.status.setText(c.getStatusText());
            }
        }else{
            if(holder instanceof FootViewHolder){
                FootViewHolder footViewHolder=(FootViewHolder)holder;
                if(!context.hasNext){
                    footViewHolder.progressBar.setVisibility(View.INVISIBLE);
                    footViewHolder.textView.setText("没有更多数据");
                }else{
                    footViewHolder.progressBar.setVisibility(View.VISIBLE);
                    footViewHolder.textView.setText("加载中");
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    static class GoodsViewHolder extends RecyclerView.ViewHolder {

        TextView code;
        TextView name_good;
        TextView unit;
        TextView status;

        public GoodsViewHolder(View view) {
            super(view);
            code=(TextView) view.findViewById(R.id.code);
            name_good=(TextView) view.findViewById(R.id.name_good);
            unit=(TextView) view.findViewById(R.id.unit);
            status=(TextView) view.findViewById(R.id.status);
        }
    }
    static class FootViewHolder extends RecyclerView.ViewHolder {
        ProgressBar progressBar;
        TextView textView;

        public FootViewHolder(View view) {
            super(view);
            progressBar=(ProgressBar) view.findViewById(R.id.progressBar);
            textView=(TextView) view.findViewById(R.id.footText);
        }
    }
}
